class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        int l = 1;
        int r = 0;
        for (int pile : piles)
            if (pile > r)
                r = pile;

        int ans = 0;
        while (l <= r)
        {
            int m = l + ((r - l) >> 1);
            long t = time(piles, m);
            if (t <= h)
            {
                ans = m;
                r = m - 1;
            }
            else
                l = m + 1;
        }
        return ans;
    }

    long time(vector<int>& piles, int k)
    {
        long t = 0;
        for (int pile : piles)
        {
            long tmp = (pile + k - 1) / k;
            t += tmp;
        }
        return t;
    }
};